//
//  PPFeedBackViewController.m
//  pickpic
//
//  Created by GaoYongyue on 16/9/17.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPFeedBackViewController.h"
#import "PPNetworkService.h"

@interface PPFeedBackViewController ()<UITextViewDelegate>
@property (nonatomic, weak) IBOutlet UITextView *textView;
@property (nonatomic, weak) IBOutlet UILabel *wordsCountLabel;
@property (nonatomic, weak) IBOutlet UIView *containerView;
@property (nonatomic,assign) NSTimeInterval time;
@end

@implementation PPFeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"意见反馈";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"设置" image:@"nav_back" target:self action:@selector(popViewPreviousController)];
    self.automaticallyAdjustsScrollViewInsets = NO;
//    [_containerView addTopBorderWithColor:kLineColor andWidth:kLineWidth];
//    [_containerView addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
    
    __weak typeof(self)weakSelf = self;
    [self setNavigationRightTitle:@"发送" titleColor:BLACK action:^{
        [weakSelf commitFeedback];
    }];
    [self textViewDidChange:_textView];
    
    self.wordsCountLabel.hidden = YES;
    
    self.view.backgroundColor = WHITE;
}

- (void)commitFeedback
{
    if ([_textView.text length] == 0 || [_textView.text isEmptyOrWhitespace])
    {
        [self.navigationController.view toastMessage:@"内容不能为空" duration:1.f];
        return;
    }
    if ([_textView.text isContainsEmoji])
    {
        [self.navigationController.view toastMessage:@"内容不能包含表情哦" duration:1.f];
        return;
    }

    NSRange range = [_textView.text rangeOfString:@" "];
    if (range.location != NSNotFound) {
        [self.navigationController.view toastMessage:@"内容不能包含空格哦" duration:1.f];
        return;
    }
    if ([_textView.text length] > 200)
    {
        [self.navigationController.view toastMessage:@"内容不能超过200字哦" duration:1.f];
        return;
    }

    //最多200个汉字，不可以包含空格和表情
    __weak typeof(self)weakSelf = self;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkService commitFeedbackWithContent:_textView.text callback:^(id data, NSError *error) {
        [hud hide:NO];
        if (data)
        {
            [weakSelf.view toastMessage:@"发送成功" duration:1 completion:^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }];
        }
        else
        {
            [weakSelf.view toastMessage:error.networkPrompt duration:1.f];
        }
    }];
}


#pragma UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    self.navigationItem.rightBarButtonItem.enabled = [textView.text length] ? YES : NO;
    NSInteger wordsCount = [textView.text length];
    if (wordsCount > 200)
    {
        _wordsCountLabel.textColor = [UIColor redColor];
    }
    else
    {
        _wordsCountLabel.textColor = [UIColor colorWithRGBHex:0xb2b2b2];
    }

    _wordsCountLabel.text = [NSString stringWithFormat:@"%ld/200", (long)( wordsCount)];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    BOOL isReturn = [text isEqualToString:@"\n"];
    if (isReturn){
        [self commitFeedback];
    }
    return !isReturn;
}


//MARK:键盘
//- (void)keyBoardWasShow:(NSNotification *)notice{
//    NSDictionary *dict=notice.userInfo;
//    //获取时间
//    if(_time==0){
//        NSTimeInterval time=[dict[UIKeyboardAnimationDurationUserInfoKey] floatValue];
//        _time=time;
//    }
//    
//    [UIView animateWithDuration:_time animations:^{
//
//        
//    }];
//}
//- (void)keyboardWillBeHidden:(NSNotification *)notice{
//    
//    [UIView animateWithDuration:_time animations:^{
//
//    }];
//}

@end
